package com.vitabuybuy.tv.ui.home.group.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.inter.OnIdItemClickListener
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.GroupBuyRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class GroupBuyZoneViewModel : BaseViewModel() {

    val hotKeylist = MutableLiveData<MutableList<HotKeyDto>>()
    fun gethotKey() {
        rxHttpRequest {
            onRequest = {
                hotKeylist.value = ShopCenterRepository.gethotKey().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val goodslist = MutableLiveData<MutableList<GroupBuyListDto>>()

    fun getGoodsList(categoryId: Int, groupType: String, hotkey: String?, pageNumber: Int) {
        val map = HashMap<String, Any>()
        if (categoryId != -1) map["category"] = mapOf(Pair("id", categoryId))
        if (groupType.isNotEmpty()) map["groupBuying"] = mapOf(Pair("type", groupType))
        if (!hotkey.isNullOrEmpty()) map["keyword"] = hotkey
        rxHttpRequest {
            onRequest = {
                goodslist.value = GroupBuyRepository.getGroupBuyList(map, pageNumber).await()
                goodslist.value?.run {
                    defUI.msgEvent.postValue(MessageModel(1, arg1 = this.size))
                }
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val memberlist = MutableLiveData<MutableList<GroupBuyMemberDto>>()
    fun getGroupBuyMember(goodId: Int) {
        rxHttpRequest {
            onRequest = {
                memberlist.value = GroupBuyRepository.getGroupBuyMember(goodId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }


    val goodDetailDto = MutableLiveData<GroupBuyDetailDto>()

    fun getGoodsDetail(goodsId: Int?) {
        rxHttpRequest {
            onRequest = {
                goodDetailDto.value = GroupBuyRepository.getGroupBuyDetail(goodsId).await()
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    fun collectOrcancel(id: Int, isFavorites: Boolean) {
        rxHttpRequest {
            onRequest = {
                GroupBuyRepository.collectOrcancel(id, isFavorites).await()
                defUI.msgEvent.postValue(MessageModel(2, obj = isFavorites))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val webContent = MutableLiveData<AgreementDto>()
    fun loadWebViewContet(id: Int?) {
        rxHttpRequest {
            onRequest = {
                webContent.value = AuthRepository.getAgreement(id).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    val recommendlist =
        MutableLiveData<MutableList<GoodsRecommendAndSimilerDto.RecommendProductListBean>>()
    val similerlist =
        MutableLiveData<MutableList<GoodsRecommendAndSimilerDto.SimilarProductListBean>>()

    val itemOnClickListener = object : OnIdItemClickListener {
        override fun onItemClick(goodId: Int) {
            defUI.msgEvent.postValue(MessageModel(3, arg1 = goodId))
        }
    }
    val recommendBinding: OnItemBind<GoodsRecommendAndSimilerDto.RecommendProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsRecommendAndSimilerDto.RecommendProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_commodity_3).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    val similerBinding: OnItemBind<GoodsRecommendAndSimilerDto.SimilarProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsRecommendAndSimilerDto.SimilarProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_commodity_4).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getGoodsList(goodId: Int) {
        rxHttpRequest {
            onRequest = {
                val it = ShopCenterRepository.getGoodsRecommendAndSimilerList(goodId).await()
                recommendlist.value = it.recommendProductList
                similerlist.value = it.similarProductList
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
